{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<script>\n",
       "code_show=true; \n",
       "function code_toggle() {\n",
       " if (code_show){\n",
       " $('div.input').hide();\n",
       " } else {\n",
       " $('div.input').show();\n",
       " }\n",
       " code_show = !code_show\n",
       "} \n",
       "$( document ).ready(code_toggle);\n",
       "</script>\n",
       "<form action=\"javascript:code_toggle()\"><input type=\"submit\" value=\"Click here to toggle on/off the raw code for QISKit exercises.\"></form>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from IPython.display import HTML\n",
    "\n",
    "HTML('''<script>\n",
    "code_show=true; \n",
    "function code_toggle() {\n",
    " if (code_show){\n",
    " $('div.input').hide();\n",
    " } else {\n",
    " $('div.input').show();\n",
    " }\n",
    " code_show = !code_show\n",
    "} \n",
    "$( document ).ready(code_toggle);\n",
    "</script>\n",
    "<form action=\"javascript:code_toggle()\"><input type=\"submit\" value=\"Click here to toggle on/off the raw code for QISKit exercises.\"></form>''')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 9. Quantum cryptography\n",
    "\n",
    "The advent of quantum computation, which introduces the possibility of using quantum mechanics for information processing, gave rise to the following question: can quantum information be shared more securely than classical information?\n",
    "\n",
    "In 1982, a very interesting property of quantum states was discovered [1,2]. This is the so-called \"no-cloning theorem\", which proved how the laws of quantum mechanics prohibit the copy of an unknown quantum state. Therefore, the no-cloning theorem assures us that qubits can hide the quantum information better than classical bits. \n",
    "\n",
    "This has important implication for example for secure communications, where it allows for the sharing of private keys which cannot be eavesdropped by a third party. We consider the first protocol, the BB84 protocol, which exploits the quantum mechanical properties of qubits for secure exchange of a secret key between two parties. \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9.1 No-cloning theorem\n",
    "\n",
    "Let us prove the no-cloning theorem, the fact that an unknown quantum state cannot be copied.\n",
    "First let us clearly state our problem:\n",
    "\n",
    "We have a qubit in an unknown quantum state $\\lvert \\psi \\rangle$ and we wish to copy his state on another qubit initilized to the state $\\lvert s \\rangle$. Therefore, we want to implement the following quantum gate:\n",
    "\n",
    "\\begin{equation}\n",
    "U\\lvert \\psi \\rangle \\lvert s \\rangle =\\lvert \\psi \\rangle \\lvert \\psi \\rangle \n",
    "\\end{equation}\n",
    "\n",
    "Let us take the unknown quantum state to be \n",
    "\n",
    "\\begin{equation}\n",
    "\\lvert \\psi \\rangle  =\\alpha \\lvert 0 \\rangle + \\beta \\lvert 1 \\rangle \n",
    "\\end{equation}\n",
    "\n",
    "where the amplitudes $\\alpha$ and $\\beta$ are unknown.\n",
    "Therefore we have:\n",
    "\n",
    "\\begin{equation}\n",
    "U\\lvert \\psi \\rangle \\lvert s \\rangle =\\lvert \\psi \\rangle \\lvert \\psi \\rangle = (\\alpha \\lvert 0\\rangle +\\beta \\lvert 1\\rangle) (\\alpha \\lvert 0\\rangle +\\beta \\lvert 1\\rangle) = (\\alpha^2 \\lvert 0\\rangle \\lvert 0\\rangle + \\alpha \\beta \\lvert 0\\rangle \\lvert 1\\rangle +   \\beta \\alpha \\lvert 1 \\rangle \\lvert 0\\rangle + \\beta^2 \\lvert 1\\rangle \\lvert 1\\rangle)\n",
    "\\tag{1}\n",
    "\\end{equation}\n",
    "\n",
    "\n",
    "Because of the linearity of operators, we can equivalently write:\n",
    "\n",
    "\\begin{equation}\n",
    "U\\lvert \\psi \\rangle \\lvert s \\rangle = U(\\alpha \\lvert 0\\rangle + \\beta \\lvert 1\\rangle )\\lvert s\\rangle = U(\\alpha \\lvert 0\\rangle \\lvert s\\rangle + \\beta \\lvert 1\\rangle \\lvert s\\rangle )=\\alpha \\lvert 00\\rangle +\\beta \\lvert 11\\rangle \n",
    "\\tag{2}\n",
    "\\end{equation}\n",
    "\n",
    "Comparing Eqs. (1) and (2), one can see that we come to a contraddiction! Thus, the operation $U$ which copies an unknown quantum state of a qubit onto another qubit is not possible."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9.2 BB84 protocol\n",
    "\n",
    "<img src=\"figures/9/bb841.jpeg\"  width=\"600\">\n",
    "$$\\text{1. BB84 protocol overview.}$$\n",
    "\n",
    "In Ref. [3], the first protocol for the distribution of a secret quantum key between two parties is described.\n",
    "\n",
    "First, let us assume that Alice and Bob may exchange qubits and classical information. Also, Alice can prepare a qubit in the $\\lvert 0 \\rangle$, $\\lvert 1 \\rangle$, $\\lvert + \\rangle = \\frac{1}{\\sqrt{2}} \\left( \\lvert 0 \\rangle + \\lvert 1 \\rangle\\right)$ and $\\lvert - \\rangle = \\frac{1}{\\sqrt{2}} \\left( \\lvert 0 \\rangle - \\lvert 1 \\rangle\\right)$ state, and Bob can measure in the standard (Z) $\\left\\{ \\lvert 0 \\rangle, \\lvert 1 \\rangle \\right\\}$ basis and in the Hadamard (H) $\\left\\{ \\lvert + \\rangle, \\lvert - \\rangle \\right\\} $ basis. Note that the two bases are non-orthogonal with respect to each other. Measuring in the $\\left\\{ \\lvert + \\rangle, \\lvert - \\rangle \\right\\} $ basis means that before the standard measurement in the $\\left\\{ \\lvert 0 \\rangle, \\lvert 1 \\rangle \\right\\} $ basis, Bob applies the Hadamard gate to the qubit. Thus\n",
    "\n",
    "\\begin{equation}\n",
    "\\lvert + \\rangle =\\frac{1}{\\sqrt{2}}(\\lvert 0\\rangle +\\lvert 1\\rangle )\n",
    "\\end{equation}\n",
    "\n",
    "gives $\\lvert 0 \\rangle$ when measured in the Hadamard basis, and\n",
    "\n",
    "\\begin{equation}\n",
    "\\lvert - \\rangle =\\frac{1}{\\sqrt{2}}(\\lvert 0\\rangle -\\lvert 1\\rangle )\n",
    "\\end{equation}\n",
    "\n",
    "gives $\\lvert 1 \\rangle$ when measured in the Hadamard basis.\n",
    "\n",
    "\n",
    "\n",
    "The protocol then works in the following way. Alice picks the bit that she wants to transmit to Bob, either $0$ or $1$. She then prepares a qubit in the corresponding state $\\lvert 0 \\rangle$ or $\\lvert 1 \\rangle$, respectively. After that, she randomly decides whether or not to transform her qubit from the standard (Z) basis to the Hadamard (H) basis by applying or not the Hadamard gate her qubit, thus preparing the state $\\lvert + \\rangle$ or $\\lvert - \\rangle$. \n",
    "\n",
    "Then Alice sends her first qubit to Bob. Bob receives Alice's qubit, selects one of the measurement bases at random and measures it. After that, Alice and Bob tell each other which basis they used through a classical communication channel. \n",
    "\n",
    "In general, for every qubit Alice sends to Bob there are four possible scenarios:\n",
    "\n",
    "<ol>\n",
    "<li>\n",
    "Both Alice and Bob used the Hadamard basis.\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "They both used the standard basis.\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "Alice transformed to the Hadamard basis, and Bob measured in the standard basis.\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "Alice used the standard basis, and Bob the Hadamard basis.\n",
    "</li>\n",
    "</ol>\n",
    "\n",
    "When Alice and Bob agree on the same basis, they keep the transferred bit. When they disagree, they discard it. Thus, it is possible for Alice and Bob to securely communicate an $n$ bit private key using $2n$ qubits.\n",
    "\n",
    "\n",
    "#### Example \n",
    "\n",
    "For example, let us consider the case where Alice wants to send the bit $0$. She prepares her qubit in the $\\lvert 0 \\rangle$ state and then randomly selects whether or not she applies the Hadamard gate to it. Let's say she does apply the Hadamard gate to her qubit, obtaining the $\\lvert + \\rangle$ state. \n",
    "\n",
    "Then, consider, the cased where Bob measures the qubit in the standard basis. After Bob's measurement, Alice and Bob communicate through the classical channel. Alice tells Bob that she  applied the Hadamard gate to her qubit and Bob tells Alice that he measured it in the standard basis. So, they abandon the first bit.\n",
    "\n",
    "<img src=\"figures/9/bb84_ex1.jpeg\"  width=\"600\">\n",
    "$$\\text{2. Example of one application of the BB84 protocol. In this case, Alice and Bob will discard this bit.}$$\n",
    "\n",
    "\n",
    "Next, Alice picks a second bit, $1$, encodes it into a qubit and selects at random whether to apply or not the Hadamard gate. Let us now assume that she does not apply the Hadamard gate. Thus, the qubit is in the state $\\lvert 1\\rangle $. Alice then sends her qubit to Bob. Bob selects at random one of his two measurement bases. Let us consider in this\n",
    "case that he measures in the standard basis. As the qubit is in the state $\\lvert 1\\rangle $ the outcome of the measurement will be $1$. Thus, Bob chooses value $1$ for his second classical bit, the same as Alice did. Finally, Alice tells Bob that she did not apply the Hadamard gate, and Bob tells Alice that he measured in the standard basis. So, both Alice and Bob will use the bit with the value $1$ as the first bit in their secret key.\n",
    "\n",
    "\n",
    "<img src=\"figures/9/bb84_ex_21.jpeg\"  width=\"600\">\n",
    "$$\\text{3. Example of another application of the BB84 protocol.} \\\\  \\text{In this case, Alice and Bob successfully communicate the value of a bit.}$$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:blue\"> QISKit: BB84 protocol </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:blue\"> 1) Show the communication of one bit </span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List of gates:\n",
      "x\n",
      "h\n",
      "measure\n",
      "\n",
      "The measured outcomes of the circuits are: {'0': 1}\n",
      "Alice and Bob don't agree the same basis, thus they discard the bit\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/anaconda/lib/python3.6/site-packages/qiskit/backends/local/qasm_simulator_cpp.py:89: DeprecationWarning: The behavior of getting statevector from simulators by setting shots=1 is deprecated and will be removed. Use the local_statevector_simulator instead, or place explicit snapshot instructions.\n",
      "  DeprecationWarning)\n"
     ]
    }
   ],
   "source": [
    "from initialize import *\n",
    "import random\n",
    "\n",
    "#initialize quantum program\n",
    "my_alg = initialize(circuit_name = 'bb84', qubit_number=1, bit_number=1, backend = 'local_qasm_simulator', shots = 1)\n",
    "\n",
    "#add gates to the circuit\n",
    "\n",
    "# Alice encodes the bit 1 into a qubit\n",
    "my_alg.q_circuit.x(my_alg.q_reg[0])\n",
    "\n",
    "# Alice randomly applies the Hadamard gate to go to the Hadamard basis\n",
    "a = random.randint(0,1)\n",
    "if a==1:\n",
    "    my_alg.q_circuit.h(my_alg.q_reg[0])\n",
    "    \n",
    "# Bob randomly applies the Hadamard gate to go to the Hadamard basis\n",
    "b = random.randint(0,1)\n",
    "if b==1:\n",
    "    my_alg.q_circuit.h(my_alg.q_reg[0])\n",
    "\n",
    "my_alg.q_circuit.measure(my_alg.q_reg[0], my_alg.c_reg[0]) # measures first qubit\n",
    "\n",
    "# print list of gates in the circuit\n",
    "print('List of gates:')\n",
    "for circuit in my_alg.q_circuit:\n",
    "    print(circuit.name)\n",
    "\n",
    "#Execute the quantum algorithm\n",
    "result = my_alg.Q_program.execute(my_alg.circ_name, backend=my_alg.backend, shots= my_alg.shots)\n",
    "\n",
    "#Show the results obtained from the quantum algorithm \n",
    "counts = result.get_counts(my_alg.circ_name)\n",
    "\n",
    "print('\\nThe measured outcomes of the circuits are:',counts)\n",
    "\n",
    "if a == b:\n",
    "    print('Alice and Bob agree on the basis, thus they keep the bit')\n",
    "else: \n",
    "    print(\"Alice and Bob don't agree the same basis, thus they discard the bit\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "### <span style=\"color:blue\"> QISKit: EPR protocol </span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#### <span style=\"color:blue\"> 3) Show the communication of one bit </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercises\n",
    "\n",
    "\n",
    "<ol>\n",
    "\n",
    "<li>\n",
    "Alice wants to send Bob the following private key\n",
    "\n",
    "\\begin{equation}\n",
    "101011\n",
    "\\end{equation}\n",
    "\n",
    "She encodes those bits into the correspondig states of qubits and performs the gates H-H-I-I-I-H on each qubit. Bob measures the qubits in the following bases: Z-H-H-Z-H-H\n",
    "\n",
    "<ol>\n",
    "<li> \n",
    "Find the possible outcomes of Bob's measurements\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "Find the bits of the private key accepted by Alice and Bob\n",
    "</li>\n",
    "</ol>\n",
    "\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "Imagine that a third party, Eve, intercepts Alice's qubits. She measures the intercepted qubit by randomly selecting either the Hadamard or the standard basis and then forwards the qubits to Bob.\n",
    "\n",
    "<ol>\n",
    "<li> \n",
    "Is it possible for Eve to find out the bit that Alice is sending to Bob without being discovered?\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "What is the probability that Eve successfully finds out the value of a bit?</li>\n",
    "</ol>\n",
    "\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "Write a QISKit program for the transmission of a 1024 bits provate key between Alice and Bob\n",
    "</li>\n",
    "\n",
    "</ol>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## References\n",
    "\n",
    "[1] D. Dieks, Physics Letters A, 92, 271 (1982).\n",
    "\n",
    "[2] W. K. Wootters and W. H. Zurek, Nature, 299.802 (1982).\n",
    "\n",
    "[3] C. H. Bennett and G. Brassard, In Proceedings of IEEE International Conference on Computers, Systems and Signal Processing, volume 175, page 8. New York, 1984."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
